package org.muyun.controllers;

import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class ParameterTestController {

    // @PathVariable 获取路径值
    @GetMapping("/pathvariable/car/{id}")
    public Map<String,Object> getCar1(@PathVariable("id") Integer id){
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        return map;
    }

    // @RequestHeader 含参获取某一请求头
    @GetMapping("/aheader/car/{id}")
    public Map<String,Object> getCar2(@PathVariable("id") Integer id,
                                      @RequestHeader("User-Agent") String agent){
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        map.put("User-Agent",agent);
        return map;
    }

    //@RequestHeader 不含参获取全部请求头
    @GetMapping("/allHeaders/car/{id}")
    public Map<String,Object> getCar3(@PathVariable("id") Integer id,
                                      @RequestHeader("User-Agent") String agent,
                                      @RequestHeader Map<String,Object> httpHeader){
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        map.put("User-Agent",agent);
        map.put("HttpHeaders",httpHeader);
        return map;
    }

    //@RequestParam 获取请求参数
    @GetMapping("/requestparam/car/{id}/owner/{user}")
    public Map<String,Object> getCar4(@RequestParam("age") Integer age,
                                      @RequestParam("inters") List<String> inters,
                                      @PathVariable Map<String,String> params){
        Map<String,Object> map = new HashMap<>();
        map.put("age",age);
        map.put("inters",inters);
        map.put("params",params);
        return map;
    }

    //@CookieValue 获取cookie值
    /*因浏览器不显示Cookie，暂废弃
    @GetMapping("/cookie/car/{id}")
    public Map<String,Object> getCar5(@PathVariable("id") Integer id,
                                      @CookieValue Cookie cookie){
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        map.put("cookie",cookie);
        return map;
    }*/

    @PostMapping("/requestattribute")
    public Map<String,Object> postCar(@RequestBody String content){
        Map<String,Object> map = new HashMap<>();
        map.put("content",content);
        return map;
    }

    //@MatrixVariable(矩阵变量)
    /* 1.语法：/cars/sell;low=34;brand=byd,audi,yd
        2.Spring Boot 默认禁用矩阵变量
        3.手动开启: 对于路径的处理，UrlPathHelper进行解析
            removeSemicolonContent(移除分号后的内容) 支持矩阵变量
        4.矩阵变量想要生效，必须要有url路径变量才能被解析
    */
    @GetMapping("/cars/lb/{path}")
    public Map carsSell1(@MatrixVariable("low") Integer low,
                        @MatrixVariable("brand") List<String> brand,
                        @PathVariable("path") String path){
        Map<String,Object> map = new HashMap<>();
        map.put("low",low);
        map.put("brand",brand);
        map.put("path",path);
        return map;
    }

    // /cars/boss/1;age=20/2;age=10
    @GetMapping("/boss/{bossid}/{employeeid}")
    public Map carsSell1(@MatrixVariable(value = "age",pathVar = "bossid") Integer bossId,
                         @MatrixVariable(value = "age",pathVar = "employeeid") Integer employeeId){
        Map<String,Object> map = new HashMap<>();
        map.put("boosAge",bossId);
        map.put("emploueeAge",employeeId);
        return map;
    }

    //   /car/2/owner/fq
    @GetMapping("/restful/car/{id}/owner/{username}")
    public Map<String,Object> getCarinfo(@PathVariable("id") Integer id,
                                     @PathVariable("username") String name,
                                     @PathVariable Map<String,String> pv){
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        map.put("username",name);
        map.put("map",pv);
        System.out.println(map);
        System.out.println(pv);
        return map;
    }

}